const express = require('express')
const api = express.Router()
const MongoClient = require('mongodb').MongoClient
const url = 'mongodb://localhost:27017/adv'
const dashboardSchema = require('../model/dashboardSchema')
const menuSchema = require('../model/menuSchema')
const advListSchema = require('../model/advListSchema')

MongoClient.connect(url,{ useNewUrlParser: true }, function(err, db) {
  if (err) throw err
  const dbo = db.db('adv')

  // 查询dashboard数据  
  api.get('/dashboard', function(req, res) {
    dashboardSchema.find({}, function(err, result) {
      if(err) throw err      
      res.send(result)
    })
  })

  // 查询菜单  
  api.get('/menu', function(req, res) {  
    menuSchema.find({}, function(err, result) {
      if(err) throw err
      res.send(result)
    })
  })

  // 查询列表 
  api.get('/advList', function (req, res) {      
    advListSchema.find({}, function(err, result) { // 返回集合中所有数据
      if (err) throw err
      res.send(result)
    })    
  })      


})

module.exports = api
